﻿using Quartz;
using System.IO;
using System.Threading.Tasks;
using System;
using FRMS.DAL;
using System.Collections.Generic;
using FRMS.Model;
using FRMS.BUS;
using Newtonsoft.Json;

namespace FRMS.TimedTask
{

    public class GetAuditData : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                ProjectDAL projectDAL = new ProjectDAL();
                List<ProjectInfo> infos = projectDAL.QueryAllProject();
                if (infos != null && infos.Count > 0)
                {
                    foreach (var item in infos)
                    {
                        VoucherBUS voucherBUS = new VoucherBUS();
                        UnSyncAudit unSyncAudit = new UnSyncAudit();
                        unSyncAudit.method = "QueryUnSyncAudit";
                        unSyncAudit.ProjectKey = item.project_key;
                        string re = voucherBUS.HttpClientPost(item.server_addr + "/api/Request/QueryUnSyncAudit", unSyncAudit);
                        UnSyncAuditReturn qRCodeReturn = JsonConvert.DeserializeObject<UnSyncAuditReturn>(re);
                        if (qRCodeReturn != null && qRCodeReturn.data != null && qRCodeReturn.method == "QueryUnSyncAudit")
                        {
                            string id = string.Empty;

                            RecordDAL recordDAL = new RecordDAL();
                            foreach (var qrdata in qRCodeReturn.data)
                            {
                                int auditid = recordDAL.QueryAuditID(qrdata.id);
                                if (auditid == 0)
                                {
                                    bool result = recordDAL.AddAuditInfo(qrdata, item.ID);
                                    if (result)
                                    {
                                        id += qrdata.id + ",";
                                    }
                                }
                                else
                                {
                                    bool result = recordDAL.UpdateAuditInfo(qrdata);
                                    if (result)
                                    {
                                        id += qrdata.id + ",";
                                    }
                                }
                            }
                            if (!string.IsNullOrEmpty(id))
                            {
                                UpdateUnSyncAuditFlag updateUnSync = new UpdateUnSyncAuditFlag();
                                updateUnSync.method = "UpdateSyncAuditFlag";
                                updateUnSync.ids = id.Substring(0, id.Length - 1);
                                updateUnSync.ProjectKey = item.project_key;
                                voucherBUS.HttpClientPost(item.server_addr + "/api/Request/UpdateSyncAuditFlag", updateUnSync);
                            }
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                //LogHelper.Log(ex.ToString());
            }
        }
    }
}
